package com.example.triprec.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.triprec.R;
import com.example.triprec.entity.Comment;
import com.example.triprec.entity.User;
import com.gzone.university.adapter.ItemViewHolder;
import com.gzone.university.adapter.ViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class ScenicDetailCommentAdapter extends ViewAdapter<ScenicDetailCommentAdapter.ViewHolder> {


    private List<Comment> list = new ArrayList<>();

    public ScenicDetailCommentAdapter(View view) {
        super(view);
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder) {
        Comment item = list.get(holder.getAdapterPosition());
        User user = item.getUser();
        holder.tvUsername.setText(user.getUsername());
        holder.tvRating.setText(String.format("%.1f", item.getScore()));
        holder.ratingBar.setRating(item.getScore().floatValue());
        holder.tvCommentTime.setText(item.getCreateTime());
        holder.tvCommentContent.setText(item.getContent());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setData(List<Comment> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends ItemViewHolder {
        private final TextView tvUsername;
        private final TextView tvRating;
        private final TextView tvCommentTime;
        private final TextView tvCommentContent;
        private final RatingBar ratingBar;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvRating = itemView.findViewById(R.id.tv_rating);
            tvCommentTime = itemView.findViewById(R.id.tv_comment_time);
            tvCommentContent = itemView.findViewById(R.id.tv_comment_content);
            ratingBar = itemView.findViewById(R.id.rating_bar);

        }
    }
} 